<!--
描述：试卷管理-自动组卷
开发人：fwb
开发日期：2018年05月22日
-->

<template lang="pug">
    div.kalix-article
        keep-alive
            el-row.frame-row(:gutter="10")
                el-col.s-flex.flex-column.frame-col(v-bind:span="20")
                    div.s-flex_item.frame-item
                        keep-alive
                            kalix-table(bizKey="adminPaper" title='试卷列表' v-bind:targetURL="paperTargetURL"
                            v-bind:bizDialog="paperBizDialog" v-bind:isFixedColumn="isFixedColumn"
                            v-bind:toolbarBtnList="paperToolbarBtnList" v-bind:btnList="paperBtnList" v-bind:customTableTool="paperCustomTableTool"
                            v-on:onTableRowClick="onTableRowClick" v-on:afterDialogClose="dialogClose"  ref="paperTable" v-bind:jsonStr="jsonStr")
                                template(slot="tableColumnSlot")
                                    el-table-column(prop="year" label="年份" align="center")
                                    el-table-column(prop="title" label="试卷名称" align="center")
                                    el-table-column(prop="totalMark" label="总分" align="center")
                                    el-table-column(prop="copies" label="自动成卷份数" align="center")
                    div.s-flex_item.frame-item
                        keep-alive
                            kalix-table(bizKey="adminMenu" title='参数列表' v-bind:targetURL="ruleTargetURL"
                            v-bind:bizDialog="ruleBizDialog" v-bind:isFixedColumn="isFixedColumn"
                            v-bind:toolbarBtnList="ruleToolbarBtnList" v-bind:customToolBar="ruleCustomToolBar"
                            v-bind:btnList="ruleBtnList" v-bind:hasTableSelection="hasTableSelection" v-bind:customTableTool="ruleCustomTableTool" v-bind:dictDefine="dictDefine"
                            ref="ruleTable" v-bind:jsonStr="jsonStr_paper" v-bind:isSpecialPage="true")
                                template(slot="tableColumnSlot")
                                    el-table-column(prop="attachmentName" label="试卷名称" align="center")
                                    <!--el-table-column(prop="attachmentSize" label="试卷大小" align="center")-->
                                    el-table-column(prop="uploadDate" label="组卷时间" align="center")
                                    el-table-column(prop="downloadStatusName" label="下载状态" align="center")
</template>

<script type="text/ecmascript-6">
    import PaperFormModel from './paperModel'
    import {AutoPaperURL, DeleteTargetURL, DownPaperURL, ReductionTargetURL} from '../config.toml'
    import {doPaperBtnList, showPaperBtnList} from './config.js'

    export default {
        name: 'kalix-enrolment-dopaper',
        data() {
            return {
                dictDefine: [{
                    cacheKey: 'ENROLMENT-DICT-KEY',
                    type: '题型顺序',
                    targetField: 'titleNumName',
                    sourceField: 'titleNum'
                }, {
                    cacheKey: 'ENROLMENT-DICT-KEY',
                    type: '题型',
                    targetField: 'quesTypeName',
                    sourceField: 'quesType'
                }],
                jsonStr: '{"delFlag": "0"}',
                jsonStr_paper: '{"mainId": "0"}',
                // 试卷规则和试题规则列表
                isFixedColumn: true,
                hasTableSelection: true,
                // 试卷规则列表
                paperTargetURL: AutoPaperURL,
                deleteTargetURL: DeleteTargetURL,
                reductionTargetURL: ReductionTargetURL,
                paperToolbarBtnList: [
                    {id: 'add', isShow: false}
//                    {id: 'autoDoPaper', title: '自动组卷', isShow: true, icon: 'icon-add'},
//                    {id: 'newAdd', title: '手动组卷', isShow: true, icon: 'icon-add'}
                ],
                paperBtnList: doPaperBtnList,
                paperBizDialog: [
                    {id: 'autoDoPaper', dialog: 'DoPaperView'}
                ],
                // 试题规则列表
                ruleTargetURL: this.$KalixGlobalConfig.AttachmentURL,
                ruleToolbarBtnList: [
                    {id: 'add', isShow: false},
                    {id: 'newAdd', title: '下载', isShow: true, icon: 'icon-add'},
                    {id: 'newDelete', title: '删除', isShow: true, icon: 'icon-add'},
                    {id: 'downAnswer', title: '下载答案', isShow: true, icon: 'icon-add'}
                    // {id: 'quesReduction', title: '试题还原', isShow: true, icon: 'icon-add'}
                ],
                ruleBtnList: showPaperBtnList,
                ruleBizDialog: [
                    {id: 'newEdit', dialog: 'RuleAdd'},
                    {id: 'newAdd', dialog: 'RuleAdd'}
                ]
            }
        },
        components: {},
        created() {
        },
        mounted() {
        },
        computed: {},
        watch: {},
        methods: {
            // 试卷规则列表自定义操作栏
            paperCustomTableTool(row, btnId, that) {
                switch (btnId) {
                    case 'autoDoPaper' : {
                        this.autoDoPaper(row, that)
                        break
                    }
                    case 'manualDoPaper' : {
                        alert('手工组卷开发中...')
                        break
                    }
                }
            },
            // 添加试卷规则
            autoDoPaper(row, table) {
                let dig = table.bizDialog.filter((item) => {
                    return item.id === 'autoDoPaper'
                })
                table.whichBizDialog = dig[0].dialog
                setTimeout(() => {
                    this.paperFormModel = Object.assign({}, PaperFormModel)
                    row.targetURL = AutoPaperURL + '/' + row.id + '/rules'
                    // table.$refs.kalixDialog.$refs.kalixBizDialog.open('自动组卷', false, this.paperFormModel)
                    table.$refs.kalixDialog.open('自动组卷', false, this.paperFormModel, row)
                }, 20)
            },
            onTableRowClick(row, event, column) {
                if (this.paperRow) {
                    if (this.paperRow.id !== row.id) {
                        // 执行查询试卷组卷试题规则数据
                        this.clearTableData(2)
                        this.paperRow = row
                        this.getRuleTableData(this.paperRow.id)
                    }
                } else {
                    // 执行查询试卷组卷试题规则数据
                    this.clearTableData(2)
                    this.paperRow = row
                    this.getRuleTableData(this.paperRow.id)
                }
            },
            // 查询试题规则列表数据
            getRuleTableData(paperId) {
                if (paperId) {
                    this.jsonStr_paper = `{mainId:${paperId}}`
                    this.$refs.ruleTable.getData()
                } else {
                    this.ruleTargetURL = this.$KalixGlobalConfig.AttachmentURL + '/0'
                }
            },
            // 清除列表数据0全部，1试卷规则，2试题规则
            clearTableData(num) {
                switch (num) {
                    // 清除所有列表数据
                    case 0 :
                        this.$refs.paperTable.clearData()
                        this.$refs.ruleTable.clearData()
                        break
                    case 1 :
                        // 清除试卷规则列表数据
                        this.$refs.paperTable.clearData()
                        break
                    case 2 :
                        // 清除试题规则列表数据
                        this.$refs.ruleTable.clearData()
                        break
                    default :
                        this.$refs.moduleTable.clearData()
                        this.$refs.menuTable.clearData()
                        break
                }
            },
            // 菜单列表自定义工具栏
            ruleCustomToolBar(btnId, table) {
                switch (btnId) {
                    case 'newAdd': {
                        this.onRuleNewAdd(table)
                        break
                    }
                    case 'newDelete': {
                        this.onRulenewDelete(table)
                        break
                    }
                    case 'downAnswer': {
                        this.onRuleNewAnswer(table)
                        break
                    }
                }
            },
            // 下载试卷
            onRuleNewAdd(table) {
                if (table.deleteList) {
                    let ids = []
                    table.deleteList.forEach(item => {
                        ids.push(item.id)
                    })
                    let newWin = window.open()
                    // 设置样式为隐藏，打开新标签再跳转页面前，如果有可现实的表单选项，用户会看到表单内容数据
                    let url = this.$KalixGlobalConfig.baseURL + DownPaperURL
                    let formStr = '<form style="visibility:hidden;" method="POST" action="' + url + '">' +
                        '<input type="hidden" name="classname" value="com.kalix.enrolment.question.biz.DownloadPaperServlet" />' +
                        '<input type="hidden" name="filetype" value="zip" />' +
                        '<input type="hidden" name="ids" value="' + ids.join(',') + '" />' +
                        '<input type="hidden" name="paperId" value="' + this.paperRow.id + '" />' +
                        '<input type="hidden" name="access_token" value="' + this.$KalixCatch.get('access_token') + '" />' +
                        '<input type="hidden" name="sessionId" value="' + this.$KalixCatch.get('user_token') + '" />' +
                        '</form>'
                    newWin.document.body.innerHTML = formStr
                    newWin.document.forms[0].submit()
                    //window.open(this.$KalixGlobalConfig.baseURL + DownPaperURL + '&ids=' + ids + '&paperId=' + this.paperRow.id + '&access_token=' + this.$KalixCatch.get('access_token') + '&sessionId=' + this.$KalixCatch.get('user_token'))
                    // this.axios.request({
                    //     headers: {
                    //         'Content-Type': 'text/plain'
                    //     },
                    //     method: 'POST',
                    //     url: DownPaperURL,
                    //     params: {},
                    //     data: {
                    //         'classname': 'com.kalix.enrolment.question.biz.DownloadPaperServlet',
                    //         'filetype': 'zip',
                    //         'ids': ids.join(','),
                    //         'paperId': this.paperRow.id,
                    //         'access_token': this.$KalixCatch.get('access_token'),
                    //         'sessionId': this.$KalixCatch.get('user_token')
                    //     }
                    // }).then(response => {
                    // })
                } else {
                    this.$KalixMessage.info('请选择数据！')
                }
            },
            // 菜单列表自定义操作栏
            ruleCustomTableTool(row, btnId, that) {
                switch (btnId) {
                    case 'downloadPaper' : {
                        this.onRuleNewEdit(row, that)
                        break
                    }
                    case 'deletePaper' : {
                        this.onRuleNewDelete(row, that)
                        break
                    }
                }
            },
            quesReduction(table) {
                let ids = []
                table.deleteList.forEach(item => {
                    ids.push(item.id)
                })
                this.$confirm('确定要还原试题吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    const loading = this.$loading({
                        lock: true,
                        text: '试题还原中，请稍等...',
                        spinner: 'el-icon-loading',
                        background: 'rgba(255, 255, 255, 0.7)'
                    })
                    this.axios.request({
                        method: 'post',
                        url: this.reductionTargetURL,
                        params: {id: ids.join(':')},
                        data: {}
                    }).then(response => {
                        table.getData()
                        this.$KalixMessage.success(response.data.msg)
                        loading.close()
                    })
                }).catch(() => {
                })
            },
            // 删除参数
            onRulenewDelete(table) {
                if (table.deleteList) {
                    let ids = []
                    table.deleteList.forEach(item => {
                        ids.push(item.id)
                    })
                    this.$confirm('确定要删除并还原试题吗?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        const loading = this.$loading({
                            lock: true,
                            text: '试题删除并还原中，请稍等...',
                            spinner: 'el-icon-loading',
                            background: 'rgba(255, 255, 255, 0.7)'
                        })
                        this.axios.request({
                            method: 'post',
                            url: this.deleteTargetURL,
                            params: {},
                            data: {'ids': ids.join(':')}
                        }).then(response => {
                            table.getData()
                            this.$KalixMessage.success(response.data.msg)
                            loading.close()
                        })
                    }).catch(() => {
                    })
                } else {
                    this.$KalixMessage.info('请选择数据！')
                }
            },
            // 下载答案
            onRuleNewAnswer(table) {
               if(this.paperRow &&(this.paperRow.tempName==='1'||(this.paperRow.tempName==='3'&&this.paperRow.kskm==='5'))){
                if (table.deleteList) {
                    let ids = []
                    table.deleteList.forEach(item => {
                        ids.push(item.id)
                    })
                    let newWin = window.open()
                    // 设置样式为隐藏，打开新标签再跳转页面前，如果有可现实的表单选项，用户会看到表单内容数据
                    let url = this.$KalixGlobalConfig.baseURL + DownPaperURL
                    let formStr = '<form style="visibility:hidden;" method="POST" action="' + url + '">' +
                        '<input type="hidden" name="classname" value="com.kalix.enrolment.question.biz.DownloadPaperServlet" />' +
                        '<input type="hidden" name="filetype" value="zip" />' +
                        '<input type="hidden" name="ids" value="' + ids.join(',') + '" />' +
                        '<input type="hidden" name="paperId" value="' + this.paperRow.id + '" />' +
                        '<input type="hidden" name="access_token" value="' + this.$KalixCatch.get('access_token') + '" />' +
                        '<input type="hidden" name="sessionId" value="' + this.$KalixCatch.get('user_token') + '" />' +
                        '<input type="hidden" name="answer" value="answer" />' +
                        '</form>'
                    newWin.document.body.innerHTML = formStr
                    newWin.document.forms[0].submit()
                } else {
                    this.$KalixMessage.info('请选择数据！')
                }
               }else {
                   this.$KalixMessage.info('该题型没有答案！')
               }
            },
            dialogClose() {
                this.$refs.ruleTable.refresh()
            }
        }
    }
</script>

<style lang='stylus' type='text/stylus'>
    .frame-row .frame-col
        width 100%
</style>